package com.pat.common.util;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


public class CryptUtil {

	private static final String KEYSTR = "PatStan123456789";
	
	  private final static char[] mChars = "0123456789ABCDEF".toCharArray();
		
	private static final Key key = new SecretKeySpec(KEYSTR.getBytes(), "AES");
	
	public static String encrypt(String orignal){
		try {
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[]data = cipher.doFinal(orignal.getBytes());
			
			return byte2HexStr(data);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String decrypt(String encrptStr){
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] data = hexStr2Bytes(encrptStr);
			
			byte[]output = cipher.doFinal(data);
			
			return new String(output);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
     * bytes转换成十六进制字符串
     * @param b byte[] byte数组
     * @param iLen int 取前N位处理 N=iLen
     * @return String 每个Byte值之间空格分隔
     */
    public static String byte2HexStr(byte[] b){
        StringBuilder sb = new StringBuilder();
        for (int n=0; n<b.length; n++){
            sb.append(mChars[(b[n] & 0xFF) >> 4]);
            sb.append(mChars[b[n] & 0x0F]);
        }
        return sb.toString();
    }
    
    /**
     * bytes字符串转换为Byte值
     * @param src String Byte字符串，每个Byte之间没有分隔符(字符范围:0-9 A-F)
     * @return byte[]
     */
    public static byte[] hexStr2Bytes(String src){
        /*对输入值进行规范化整理*/
        src = src.toUpperCase();
        //处理值初始化
        int m=0,n=0;
        int iLen=src.length()/2; //计算长度
        byte[] ret = new byte[iLen]; //分配存储空间
        
        for (int i = 0; i < iLen; i++){
            m=i*2+1;
            n=m+1;
            ret[i] = (byte)(Integer.decode("0x"+ src.substring(i*2, m) + src.substring(m,n)) & 0xFF);
        }
        return ret;
    }
    
	
	public static void main(String[] args) {
		String s1 = "12321321321";
		String c = encrypt(s1);
		System.out.println(c);
		
		System.out.println(decrypt(c));
		
		String s = "8cac9711c95ca4966a275fb42388d031760d9e45d9d857e651c82547ff134652";
		String p = decrypt(s);
		System.out.println(p);
		System.out.println(System.currentTimeMillis());
		System.out.println((System.currentTimeMillis()-Long.valueOf(p.split("_")[1]))/1000/60);
	}
	
}
